package com.scholarship.modules.backend.service;

import com.scholarship.common.utils.MyDateUtil;
import com.scholarship.common.utils.RedisUtil;
import com.scholarship.modules.backend.exce.TimeException;
import com.scholarship.modules.backend.mapper.RewardTimeMapper;
import com.scholarship.modules.login.entity.RewardTime;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author zhz
 * @date 2021/8/30 10:17
 * 备注：奖学金时间的CURD
 */
@Service
public class RewardTimeService {

    @Autowired
    RewardTimeMapper rewardTimeMapper;

    @Autowired
    private RedisUtil redisUtil;

    //增加奖学金申请时间
    public void addSubmitTime(Date start, Date end)throws Exception{
        if(start==null||end==null) throw new TimeException("时间不能为空值");
        if(start.after(end)) throw new TimeException("申请开始时间必须早于申请结束时间");
        else{
            RewardTime rewardTime = getRewardTime();
            if(rewardTime.getAppealStartTime()!=null&&end.after(rewardTime.getAppealStartTime())) throw new TimeException("申请结束时间必须早于申诉开始时间");
            rewardTimeMapper.addSubmitTime(MyDateUtil.UTCToYMD(start),MyDateUtil.UTCToYMD(end));
            redisUtil.set("rewardTime",rewardTimeMapper.getRewardTime(),0L);
        }
    }

    //增加奖学金申诉时间
    public void addAppealTime(Date start,Date end) throws Exception {
        if(start==null||end==null) throw new TimeException("时间不能为空值");
        if(start.after(end)) throw new TimeException("开始时间不能晚于结束时间");
        else{
            RewardTime rewardTime = getRewardTime();
            if(rewardTime.getSubmitEndTime().toString()!=null&&rewardTime.getSubmitEndTime().after(start))throw new TimeException("申诉开始时间必须晚于申请结束时间");
            rewardTimeMapper.addAppealTime(MyDateUtil.UTCToYMD(start),MyDateUtil.UTCToYMD(end));
            redisUtil.set("rewardTime",rewardTimeMapper.getRewardTime(),0L);
        }
    }

    //同时增加奖学金申请、申诉时间
    public void addRewardTime(RewardTime reward) throws Exception{
        if(reward.getSubmitEndTime()==null||reward.getSubmitStartTime()==null||reward.getAppealEndTime()==null||reward.getAppealStartTime()==null) throw new TimeException("时间不能为空值");
        reward.setSubmitStartTime(MyDateUtil.UTCToYMD(reward.getAppealStartTime()));
        reward.setSubmitEndTime(MyDateUtil.UTCToYMD(reward.getSubmitEndTime()));
        reward.setAppealStartTime(MyDateUtil.UTCToYMD(reward.getAppealStartTime()));
        reward.setAppealEndTime(MyDateUtil.UTCToYMD(reward.getAppealEndTime()));
        if(reward.getSubmitStartTime().after(reward.getSubmitEndTime())) throw new TimeException("申请开始时间不能晚于申请结束时间");
        else if(reward.getAppealStartTime().after(reward.getAppealEndTime())) throw new TimeException("申诉开始时间不能晚于申诉结束时间");
        else if(reward.getSubmitEndTime().after(reward.getAppealStartTime())) throw new TimeException("申诉开始时间必须晚于申请结束时间");
        rewardTimeMapper.addRewardTime(reward);
        redisUtil.set("rewardTime",rewardTimeMapper.getRewardTime(),0L);
    }

    //删除奖学金奖学金申请、申诉时间
    public void deleteRewardTime() throws Exception{
        rewardTimeMapper.deleteRewardTime();
        redisUtil.set("rewardTime",rewardTimeMapper.getRewardTime(),0L);
    }

    //删除奖学金奖学金申请时间
    public void deleteSubmitTime() throws Exception{
        rewardTimeMapper.deleteSubmitTime();
        redisUtil.set("rewardTime",rewardTimeMapper.getRewardTime(),0L);
    }

    //删除奖学金奖学金申诉时间
    public void deleteAppealTime() throws Exception{
        rewardTimeMapper.deleteAppealTime();
        redisUtil.set("rewardTime",rewardTimeMapper.getRewardTime(),0L);
    }

    //获取奖学金时间
    public RewardTime getRewardTime() throws Exception{
        //redis储存奖学金开放时间
        if(!redisUtil.exists("rewardTime")) {
            System.out.println("rewardTime is null");
            redisUtil.set("rewardTime",rewardTimeMapper.getRewardTime(),0L);
        }
//        System.out.println(redisUtil.get("rewardTime").toString());
        return redisUtil.get("rewardTime");
    }
}
